<?php App::uses('AppController', 'Controller');
/**
 * OrdenesTags Controller
 *
 * @property OrdenesTag $OrdenesTag
 */
class OrdenesTagsController extends AppController
{
    public $uses = array(
        'OrdenesTag',
        'Tag',
        'Categoria',
        'Documento',
        'Ordene');
    public $layout = 'epcm';
    public function index()
    {
        $tags = $this->Tag->find('all', array('recursive' => -1));
        $dcc = $this->Categoria->find('list', array('fields' => 'nombre'));
        $documentos = $this->Documento->find('all', array('recursive' => -1));
        //$arraycrt = 'hola';
        //debug($tags);
        $this->set(compact('tags', 'dcc', 'documentos'));
    }
    public function guardaOrden()
    {
        //debug($this->data);
        $tags = $this->Tag->find('all', array('recursive' => -1));
        if ($this->Ordene->save($this->data))
        {
            echo 'guardo';
        }
        /*foreach ($tags as $t)
        {
        $id_tag = $t['Tag']['id'];
        if ($this->data["ordenestags"]["tagSel_$id_tag"])
        {
        echo "selecciono " . $id_tag;
        } else
        {
        
        }
        }*/
    }
    function editar($id = null)
    {
       $ctag=$this->OrdenesTag->find('first', array('recursive'=>-1,
        'conditions'=>array('id'=>$id)
        ));
        $cumpita=$ctag['OrdenesTag']['ordene_id'];
        //debug($cta);  
        
         $this->OrdenesTag->id = $id;
        if (!$id)
        {
            $this->Session->setFlash('No Existe el tipo de Ordenes Tag');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data))
        {
            $this->data = $this->OrdenesTag->read();
        } else
        {
            if ($this->OrdenesTag->save($this->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente la Ordenes Tag');
                $this->redirect(array('controller' => 'Ordenes', 'action' => 'editartags',$cumpita));
            } else
            {
                $this->Session->setFlash('Error al guardar la Ordenes Tag');
            }
        }
    }
    public function eliminar($id = null)
    {
        $this->OrdenesTag->id = $id;
        $this->data = $this->OrdenesTag->read();
        if (!$id)
        {
            $this->Session->setFlash('No existe la Orden Tags a Eliminar');
            $this->redirect(array('action' => 'index'));
        } else
        {
            if ($this->OrdenesTag->delete($id))
            {
                $this->Session->setFlash('Se elimino la Orden Tags' . $this->data['Ordenes']['nombre']);
                $this->redirect(array('controller' => 'Ordenes', 'action' => 'index'));
            } else
            {
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
    function insertar($idOrden = null)
    {
        $datosOrden = $this->Ordene->find('first', array(
            'recursive' => -1, 
            'conditions' => array('id' => $idOrden) ));
        //debug($datosOrden);
        
        if (!empty($this->data))//permite diferenciar cuando recibe los datos
        {
            //debug($this->data);exit;
            if ($this->OrdenesTag->saveMany($this->data))
            {
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('controller'=>'Ordenes', 'action' => 'editartags', $idOrden));
            } else
            {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        $this->set(compact('idOrden', 'datosOrden'));
    }
}
